#ifndef _MODBUS_SLAVE_H__
#define _MODBUS_SLAVE_H__

#include "modbus_user.h"

// 硬件抽象层接口
typedef struct {
    void (*uart_send)(uint8_t *dat, uint16_t len);
    uint32_t (*get_timestamp_ms)(void);
} ModbusSlavePort;

// Modbus寄存器操作回调
typedef struct {
    // 返回: 0=成功, 非0=异常代码
    ModbusException (*read_holding_registers)(uint16_t start_addr, uint16_t reg_count, uint16_t *buffer);
    // 返回: 0=成功, 非0=异常代码
    ModbusException (*write_single_register)(uint16_t reg_addr, uint16_t value);
} SlaveCallBacks;

// Modbus从机结构体
typedef struct {
    ModbusSlavePort port;
    SlaveCallBacks callbacks;
    uint8_t slave_addr;
    uint8_t rx_buffer[256];
    uint16_t rx_index;
    uint32_t last_rx_time;
    uint8_t tx_buffer[256];
    uint16_t tx_length;
    uint8_t frame_timeout;
} SlaveConfig;

void ModbusSlaveCreate(SlaveConfig *slave, uint8_t slave_addr,ModbusSlavePort *port,
                       SlaveCallBacks *callbacks, uint8_t frame_timeout) ;
// Modbus从机轮询处理
void ModbusSlaveProcess(SlaveConfig *slave, RingBuffer_t *rx_queue) ;


extern SlaveConfig slave;

#endif  /* _MODBUS_SLAVE_H__ */
